package org.needcoke.hs.signature;

import cn.hutool.core.util.StrUtil;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 排序方法
 */
@FunctionalInterface
public interface SortedFunction {

    /**
     * 排序
     */
    List<String> sort(Collection<String> keyCollection);


    /**
     * 默认字符串升序
     */
    class DefaultSortedFunction implements SortedFunction {
        @Override
        public List<String> sort(Collection<String> keyCollection) {
            return keyCollection.stream().sorted(new Comparator<String>() {
                @Override
                public int compare(String o1, String o2) {
                    return StrUtil.compare(o1, o2, true);
                }
            }).collect(Collectors.toList());
        }
    }

    /**
     * 字符串降序
     */
    class StringDescSortedFunction implements SortedFunction{
        @Override
        public List<String> sort(Collection<String> keyCollection) {
            return keyCollection.stream().sorted(new Comparator<String>() {
                @Override
                public int compare(String o1, String o2) {
                    if (o1 == o2) {
                        return 0;
                    } else if (o1 == null) {
                        return -1;
                    } else if (o2 == null) {
                        return 1;
                    } else {
                        return o1.toString().compareTo(o2.toString());
                    }
                }
            }).collect(Collectors.toList());
        }
    }


    SortedFunction defaultSortedFunction = new SortedFunction.DefaultSortedFunction();

    SortedFunction stringDescSortedFunction = new SortedFunction.StringDescSortedFunction();
}
